layui.define([ 'form', 'laydate', 'table'], function(exports) {
    var form = layui.form;
    var table = layui.table;
    var index = layui.index;
    var view = {
        init:function(){
        	  this.initSubmit();
        },
        initSubmit:function(){
        	//设为默认地址
            $(document).on('click', '.set_up_the', function () {
                var t = $(this);
                var addressId=t.attr('areaid');
                var data = {'addressId': addressId};
                doAjax('/user/userAddress/updateDefAddr.json','post',data,function(res){
               	 location.href = location.href;
               });
            })
            
            //删除地址
            $(document).on('click', '.delete', function () {
            	 var t = $(this);
 	            var addressId=t.attr('areaid');
 	            var data = {'addressId': addressId};
 	            $.confirm('友情提示', '确定删除收货地址', function () {
 	            	doAjax('/user/userAddress/removeAddress.json','post',data,function(res){
 	                 	 location.href = location.href;
 	                 });
 	            });
            })
            
             //新增/编辑收货地址
             $(document).on('click', '.editor , .zjshdz', function () {
            	 	var t=$(this).attr('id_alter');
             		if(!t){t=0;}
	            	doAjax('/html/addOrEditAddress.html', 'get', {'addressId':0}, function (res) {
		            	if(t>0){
		            		 doAjax('/user/userAddress/getAddressInfoById.json', 'post', {'addressId':t}, function (data) {
		            			 var userAddress=data.data;
		            			 $('#areaId').val(userAddress.areaId);//收货人地区Id
		            			 $('#addressId').val(userAddress.addressId);//收货人地址Id
		            			 $('.op_splice_area_con').val(userAddress.addr);//详情地址
		            			 $('.op_splice_consignee').val(userAddress.name);//收货人
		            			 $('.op_splice_phone').val(userAddress.mobile);//手机号码
		            			 $('.op_splice_tel').val(userAddress.tel);//固定号码
		            			 $('.op_splice_zip').val(userAddress.zip);//邮编
		            			 if(userAddress.defAddr){
		            				 $('.check_add').val('1') ;
		            				 $('.ico_check').addClass('active');
		            			 }	else{
		            				 $('.check_add').val('0') ;
		            				 $('.ico_check').removeClass('active');
		            			 }												 //是否默认
		            			 
		            			 
		            		 })
		            	}
		                $.prompt({
		                    style: '',
		                    title: '个人收货地址',
		                    con: res,
		                });
		                if(t>0){
		                	 doAjax('/user/userAddress/getAddressInfoById.json', 'post', {'addressId':t}, function (data) {
		            			 var userAddress=data.data;
		            			 $.select_add("add_select_site", region_Data, userAddress.areaId);//默认北京/北京市/东城区							
		            		 })
		                	
		                }else{
		                	$.select_add("add_select_site", region_Data, 3);//默认北京/北京市/东城区
		                }
		            }, '', 'html')
            })
             //选择默认地址事件
            $(document).on('click', '.check_add', function () {
            	alert()
            })
              //确认
            $(document).on('click','.addressAdd_sub',function(){
                if (!validForm_02.check(false)) {
                    $('.invoice_bill li p').show();
                    return;
                }
                var addressId=$("#addressId").val()?$("#addressId").val():"0",//地址ID
	                name=$('.op_splice_consignee').val(),//收货人姓名：
	                areaId=$('.input_addr').val(),//收货人地区id：
	                addr=$('.op_splice_area_con').val(),//收货人地址
	                zip=$('.op_splice_zip').val(),//邮编
	                tel=$('.op_splice_tel').val(),//固定号码
	                mobile=$('.op_splice_phone').val(),//手机号码
	                defAddr=$('.check_add').val()=='1'?true:false;//是否默认
                var addressData={
                	addressId:addressId,
                	name:name,
                	areaId:areaId,
                	addr:addr,
                	zip:zip,
                	tel:tel,
                	mobile:mobile,
                	defAddr:defAddr
                }
                doAjax('/user/userAddress/saveOrUpdateAddress.json','post',{"addressData":JSON.stringify(addressData)},function(res){
                	 location.href = location.href;
                });
                $('.popup-btn-close').click();
            })
             $(document).on('click','.bill-but-false',function(){
                $('.popup-btn-close').click();
            })
            
             $(document).on('click','.qx_sub ',function(){
            	 $('.popup-btn-close').click();
            })
        }
            
    }
    exports('index',view);
	
});